package com.applozic.mobicomkit.uiwidgets.conversation.view;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.applozic.mobicomkit.ChatConfig;
import com.applozic.mobicomkit.api.conversation.Message;
import com.applozic.mobicomkit.contact.BaseContactService;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.GiveRewardDetailActivity;
import com.lottery.ruok.lottrey.common.Common;
import com.lottery.ruok.lottrey.ui.component.BaseRelativeLayout;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;
import glideHelp.GlideHelper;

public class GivePrizeRIghtView extends BaseRelativeLayout {
    @BindView(R.id.avatar)
    CircleImageView circleImageView;
    @BindView(R.id.from)
    TextView from;
    @BindView(R.id.toAvatar)
    CircleImageView toAvatar;
    @BindView(R.id.to)
    TextView to;
    private Message message;
    private BaseContactService contactService;

    public GivePrizeRIghtView(Context context) {
        super(context);
    }

    public GivePrizeRIghtView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public GivePrizeRIghtView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init(Context context, AttributeSet attrs, int defStyleAttr) {

    }

    @Override
    protected void initView(Context context) {
        setContentView(R.layout.give_prize_item_right);
        setRoundImg(circleImageView);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!UserInfo.getToken().equals("")) {
                    if (UserInfo.getIsTest() == 1 && UserInfo.getIsTry() != 1) {
                        utils.Utils.showToast(Common.IS_TEST);
                    } else {
                        if (message == null || contactService == null) {
                            return;
                        }
                        Intent intent = new Intent(mContext, GiveRewardDetailActivity.class);
                        intent.putExtra(ChatConfig.REWARD_TIME, message.getCreatedAtTime());
                        intent.putExtra(ChatConfig.REWARD_FROM, message.getMetadata().get(ChatConfig.REWARD_FROM));
                        intent.putExtra(ChatConfig.REWARD_TO, message.getMetadata().get(ChatConfig.REWARD_TO));
//                        intent.putExtra(ChatConfig.REWARD_FROM_AVATAR, UserInfo.getAvatar());
//                        intent.putExtra(ChatConfig.REWARD_TO_AVATAR, message.getMetadata().get(ChatConfig.REWARD_TO_AVATAR));
                        intent.putExtra(ChatConfig.REWARD_MONEY, message.getMetadata().get(ChatConfig.REWARD_MONEY));
                        mContext.startActivity(intent);
                    }
                } else {
                    utils.Utils.showToast("请先登录");
                }
            }
        });
    }

    @Override
    protected void load(Context context) {

    }

    public void setData(Message message, BaseContactService contactService) {
        this.message = message;
        this.contactService = contactService;
        GlideHelper.loadImage(getContext(), contactService.getContactById(message.getMetadata().get(ChatConfig.REWARD_TO)).getImageURL(), toAvatar, R.mipmap.chat_default);
        to.setText(contactService.getContactById(message.getMetadata().get(ChatConfig.REWARD_TO)).getDisplayName());
        from.setText(contactService.getContactById(message.getMetadata().get(ChatConfig.REWARD_FROM)).getDisplayName());

    }

    private void setRoundImg(ImageView myAvatar) {
        if (myAvatar != null) {
            if (UserInfo.getToken().equals("")) {
                myAvatar.setImageResource(R.mipmap.chat_default);
            } else {
                if (UserInfo.getAvatar().equals("")) {
                    myAvatar.setImageResource(R.mipmap.chat_default);
                } else {
                    GlideHelper.loadRoundImage(getContext(), UserInfo.getAvatar(), myAvatar);
                }
            }
        }
    }
}
